#include "InetAddress.h"
#include <strings.h>

InetAddress::InetAddress(const string &ip, string port){
    ::bzero(&_addr, sizeof(struct sockaddr_in));
    _addr.sin_family = AF_INET;

    // 设置端口号
    _addr.sin_port = htons(atoi(port.c_str())); 
   
    // 设置 IP 
    _addr.sin_addr.s_addr = inet_addr(ip.c_str());// host -> network 
}


InetAddress::InetAddress(const struct sockaddr_in &addr)
: _addr(addr){}


InetAddress::~InetAddress(){}


string InetAddress::ip() const{
    // 从网络字节序转换为本机字节序
    return string(inet_ntoa(_addr.sin_addr));
}


unsigned short InetAddress::port() const{
    // 从网络字节序转换为本机字节序
    return ntohs(_addr.sin_port);
}


const struct sockaddr_in *InetAddress::getInetAddrPtr() const{
    return &_addr;
}
